package ntit.csie.yongzan.MultiTouchEvent;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class MultiTouchMain extends Activity {
	private ImageView paper;
	private ImageView paper2;
	private int MotionW;
	private int MotionH;
	private PaintView paintView;
	private boolean fpoint=false;
	private boolean spoint=false;
	//private PaintView paintViewTwo;
	private Point pointSet;
	private Point pointSetTwo;
	private Handler MainHandler = new Handler();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.main);
        setfindView();
        setConfig();
    }

	private void setfindView() {
		paper=(ImageView)findViewById(R.id.ImageViewPaint);
		//paper2=(ImageView)findViewById(R.id.ImageViewPaint2);
	}

	private void setConfig() {
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		MotionW = dm.widthPixels;
		MotionH = dm.heightPixels;
		paintView=new PaintView(paper,MotionW,MotionH);
	}
	public boolean onTouchEvent(MotionEvent e) {
		switch(e.getAction()){
		case MotionEvent.ACTION_DOWN:
			fpoint=true;
			pointSet=new Point(e.getX(0),e.getY(0));
			break;
		case MotionEvent.ACTION_POINTER_1_DOWN:
			fpoint=true;
			break;
		case MotionEvent.ACTION_POINTER_2_DOWN:
			spoint=true;
			pointSetTwo=new Point(e.getX(1),e.getY(1));
			
			break;
		case MotionEvent.ACTION_MOVE:
			try{
				//Log.i("DER>",fpoint+","+spoint);
				if(fpoint&&!spoint){
					paintView.paintDrawCross(e.getX(0), e.getY(0),Color.RED);
				}else if(spoint&&!fpoint){
					paintView.paintDrawCross(e.getX(1), e.getY(1),Color.BLUE);
				}else if(spoint&&fpoint){
					paintView.paintDraw2Cross(e.getX(0), e.getY(0),Color.RED,e.getX(1), e.getY(1),Color.BLUE);
				}
			}catch(Exception ee){}
			break;
		case MotionEvent.ACTION_POINTER_1_UP:
			Log.i("DER>","ACTION_POINTER_1_UP");
			MainHandler.post(EnterClean);
			fpoint=false;
			break;
		case MotionEvent.ACTION_POINTER_2_UP:
			MainHandler.post(EnterClean);
			spoint=false;
			break;
		case MotionEvent.ACTION_UP:
			Log.i("DER>","ACTION_UP");
			MainHandler.post(EnterClean);
			fpoint=false;
			spoint=false;
			break;
		}
		return false;
	}
	 private Runnable EnterClean = new Runnable(){
		    public void run(){
				paintView.paintDrawClear();
				MainHandler.removeCallbacks(EnterClean);
				//Log.i("DER>","EnterClean");
		    }
	};//EnterClean
	//MainHandler.post(EnterClean);
	/*float r2 = (float) Math.sqrt((e.getX(1)-pointSetTwo.getX()) * (e.getX(1)-pointSetTwo.getX()) + (e.getY(1)-pointSetTwo.getY()) * (e.getY(1)-pointSetTwo.getY()))/ 2;  
	if(r2>10){
		pointSetTwo.setXY(e.getX(1),e.getY(1));
		MainHandler.post(EnterClean2);
	}
	paintView.paintDrawCross(e.getX(0), e.getY(0),Color.YELLOW);
	if(e.getPointerCount()>1){
		paintView.paintDraw2Cross(e.getX(0), e.getY(0),Color.YELLOW,e.getX(1), e.getY(1),Color.RED);
	}
	float r = (float) Math.sqrt((e.getX(0)-pointSet.getX()) * (e.getX(0)-pointSet.getX()) + (e.getY(0)-pointSet.getY()) * (e.getY(0)-pointSet.getY()))/ 2;  
	if(r>10){
		Log.i("DER>",r+"=");
		pointSet.setXY(e.getX(0),e.getY(0));
		MainHandler.post(EnterClean);
	}*/
}